<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2019/11/19
 * Time: 9:13 AM
 */

namespace App\BO\Coupon\CouponProductSkuCheck;

use Illuminate\Support\Facades\Log;

class UseCityCheck implements ICouponProductSkuCheck
{
    /**
     * 优惠券可用城市检查
     * @param CouponProductSkuCheckMeta $checkMeta
     * @param \Closure $next
     * @return CheckResultWrapper|mixed
     */
    public static function check(CouponProductSkuCheckMeta $checkMeta, \Closure $next)
    {
        $coupon = $checkMeta->getCouponEntity();
        $areaEntity = $checkMeta->getAreaEntity();

        if ($coupon->getCityId() && !in_array($areaEntity->getId(), explode(',', $coupon->getCityId()))) {
            Log::info('当前城市为' . $areaEntity->getAreaname() . '。 优惠券' . $coupon->getId() . '只能在' . $coupon->getAreaname() . '使用');

            $checkResultWrapper = new CheckResultWrapper($checkMeta->getCouponEntity());
            $checkResultWrapper->setMessage('只能在' . $coupon->getAreaname() . '使用');
            return $checkResultWrapper;
        }

        return $next($checkMeta);
    }
}
